<template>
    <div class="task-list">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>我的任务列表</span>
          <!-- 添加任务按钮 -->
          <el-button type="primary" class="add-task-btn" @click="goToAddTaskPage">添加任务</el-button>
        </div>
        <el-table :data="tasks" style="width: 100%">
          <el-table-column prop="name" label="任务名称" width="180"></el-table-column>
          <el-table-column prop="completed" label="完成状态" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.completed ? 'success' : 'info'">
                {{ scope.row.completed ? '已完成' : '未完成' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="goToEditTaskPage(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        tasks: []
      };
    },
    created() {
      this.fetchTasks();
    },
    methods: {
      fetchTasks() {
        const token = localStorage.getItem('token');
        axios.get('/api/tasks/mytasks', {
          headers: {
            'Authorization': token
          }
        })
        .then(response => {
          this.tasks = response.data;
        })
        .catch(error => {
          this.$message.error('获取任务列表失败');
        });
      },
      goToAddTaskPage() {
        this.$router.push('/add-task');
      },
      goToEditTaskPage(task) {
        this.$router.push(`/edit-task/${task.id}`);
      }
    }
  };
  </script>
  
  <style scoped>
  .task-list {
    margin: 20px;
  }
  .box-card {
    width: 500px;
  }
  .add-task-btn {
    float: right;
    margin-bottom: 10px;
  }
  </style>
  